################################################################
#
# Code to replicate figures in:
# The Exectuve Revolving Door: New dataset on the Career Moves of former Danish Ministers and Permanent Secretaries
# In: Scandinavian Political Studies
# By Anne Rasmussen, Nicholas Buhmann-Holmnes & Benjamin C.K. Egerod
# June 2021
#
################################################################


# load libraries
pacman::p_load(haven, dplyr, ggplot2, gridExtra, gtable, grid, cowplot)

# set working directory to the one containing replication materials
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))


# load data
df <- read_dta("data_pre_collapse_040621.dta")

df_collapse <- read_dta("data_post_collapse_040621.dta")


#-----------------------------------------
# Figure 1: revolving door in full period
#-----------------------------------------

# compute revolving door in senior/non-senior positions
tab_base1 <- df_collapse %>%
  group_by(pubpriv_senior) %>%
  summarise( tot = n() ) %>%
  mutate(pct = tot/sum(tot) )

p_base1 <- ggplot(tab_base1, 
                 aes(x = reorder(pubpriv_senior,pct*100), 
                     y = pct*100, 
                     fill = factor(pubpriv_senior))) +
  geom_bar(position = position_dodge(0.7), stat = "identity",
           colour = "black") +
  theme_classic() +
  coord_flip() +
  scale_fill_manual(values = c("white", "grey", "black"),
                    labels = c("Private, Non-Senior",
                               "Public",
                               "Private, Senior"),
                    name = NULL) +
  labs(x = NULL, y = NULL,
       title = "A: Post-Position Roles") +
  theme(legend.position = "none")+
  geom_text(aes(y = (pct*100)+4,
                label = paste(round(pct*100, digits = 1),"%",sep=""), group = pubpriv_senior),
            position = position_dodge(width = 0.7)) +
  geom_text(aes(y = (pct*100)+11,
                label = paste("(", tot,")",sep=""), group = factor(pubpriv_senior)),
            position = position_dodge(width = 0.7)) +
  scale_x_discrete(labels = c("Private, Non-Senior","Public", "Private, Senior"))

p_base1

# split by minister/bureaucrat
tab_base2 <- df_collapse %>%
  group_by(minst_secr, pubpriv_senior) %>%
  summarise( tot = n()) %>%
  group_by(minst_secr) %>%
  mutate(pct = tot/sum(tot))

tab_base2 <- tab_base2 %>%
  group_by(pubpriv_senior) %>%
  mutate(post_pct = tot/sum(tot))

tab_base2$pubpriv_senior <- factor(tab_base2$pubpriv_senior,
                                          level = c(1,0, 2))

p_base2 <- ggplot(tab_base2, 
                 aes(x = reorder(minst_secr,post_pct*100), 
                     y = pct*100, 
                      fill = factor(pubpriv_senior))) +
  geom_bar(position = position_dodge(0.7), stat = "identity",
           colour = "black") +
  theme_classic() +
  coord_flip() +
  scale_fill_manual(values = c("grey","white", "black"),
                    labels = c("Private, Non-Senior",
                               "Public",
                               "Private, Senior"),
                    name = NULL) +
  labs(x = NULL, y = NULL,
       title = "B: Post-Position Roles By Minister/PermSec") +
  geom_text(aes(y = (pct*100)+4,
                label = paste(round(pct*100, digits = 1),"%",sep=""), group = pubpriv_senior),
            position = position_dodge(width = 0.7)) +
  geom_text(aes(y = (pct*100)+11,
                label = paste("(", tot,")",sep=""), group = factor(pubpriv_senior)),
            position = position_dodge(width = 0.7)) +
  scale_x_discrete(labels = c("Minister","Permanent\nSecretary"),
                     breaks = c(0,1))+
  theme(legend.position = "bottom")

p_base2

#make single legend for the plots
g_legend<-function(a.gplot){
  tmp <- ggplot_gtable(ggplot_build(a.gplot))
  leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
  legend <- tmp$grobs[[leg]]
  return(legend)}

mylegend<-g_legend(p_base2)

# combine graphs and add single legend
p_base <- grid.arrange(arrangeGrob(p_base1 + theme(legend.position="none"),
                               p_base2 + theme(legend.position="none"),
                               nrow=1),bottom = "Percent Hired",
                   mylegend, nrow=2,heights=c(10, 1))

# export figure
ggsave(p_base,filename = "Figure1.tiff", device = "tiff",
       width = 12.5, height = 6, dpi = 600)

#-------------------------------
# Figure 2: immediate transition (t and t+1)
#------------------------------


tab_base3 <- df_collapse %>%
  group_by(position01) %>%
  summarise( tot = n()) %>%
  mutate(pct = tot/sum(tot))

p_base3 <- ggplot(tab_base3, 
                  aes(x = reorder(position01,pct*100), 
                      y = pct*100, 
                      fill = factor(position01))) +
  geom_bar(position = position_dodge(0.7), stat = "identity",
           colour = "black") +
  theme_classic() +
  coord_flip() +
  scale_fill_manual(values = c("grey", "black", "white"),
                    labels = c("Private, Non-Senior",
                               "Private, Senior",
                               "Public"),
                    name = NULL) + theme(legend.position = "none")+
  labs(x = NULL, y = NULL,
       title = "A: Post-Position Roles, time t and t+1") +
  geom_text(aes(y = (pct*100)+4,
                label = paste(round(pct*100, digits = 1),"%",sep=""), group = position01),
            position = position_dodge(width = 0.7)) +
  geom_text(aes(y = (pct*100)+11,
                label = paste("(", tot,")",sep=""), group = factor(position01)),
            position = position_dodge(width = 0.7)) +
  scale_x_discrete(labels = c("Private, Non-Senior",
                              "Private, Senior", "Public"))

p_base3

# split by minister/permsec

tab_base4 <- df_collapse %>%
  group_by(minst_secr, position01) %>%
  summarise( tot = n()) %>%
  group_by(minst_secr) %>%
  mutate(pct = tot/sum(tot))

tab_base4$minst_secr <- relevel(factor(tab_base4$minst_secr), ref = "1")

p_base4 <- ggplot(tab_base4, 
                 aes(x = reorder(minst_secr,pct*100), 
                     y = pct*100, 
                     fill = factor(position01))) +
  geom_bar(position = position_dodge(0.7), stat = "identity",
           colour = "black") +
  theme_classic() +
  coord_flip() +
  scale_fill_manual(values = c("grey", "black", "white"),
                    labels = c("Private, Non-Senior",
                               "Private, Senior",
                               "Public"),
                    name = NULL) +
  labs(x = NULL, y = NULL,
       title = "B: Post-Position Roles by Minister/PermSec, time t and t+1") +
  geom_text(aes(y = (pct*100)+4,
                label = paste(round(pct*100, digits = 1),"%",sep=""), group = position01),
            position = position_dodge(width = 0.7)) +
  geom_text(aes(y = (pct*100)+11,
                label = paste("(", tot,")",sep=""), group = factor(position01)),
            position = position_dodge(width = 0.7)) +
  scale_x_discrete(labels = c("Minister","Permanent\nSecretary"),
                   breaks = c(0,1))+ theme(legend.position = "bottom")

p_base4

mylegend<-g_legend(p_base4)

p_base5 <- grid.arrange(arrangeGrob(p_base3 + theme(legend.position="none"),
                                   p_base4 + theme(legend.position="none"),
                                   nrow=1),bottom = "Percent Hired",
                       mylegend, nrow=2,heights=c(10, 1))


ggsave(p_base5, filename = "Figure2.tiff",
       device = "tiff", dpi = 600,
       width = 12.5, height = 6)


#-----------------------------------------------------------
# Figure 3: Group Type
#-----------------------------------------------------------

# group type and hires t = 0 and t +1
tab1 <- df %>%
  filter(Job_t_role < 1.1 & Job_t_role > -0.1 & is.na(Jobcoincide) == T & is.na(Singlefewpersonbusinessself) ==T) %>%
  group_by(MetaIntGrReco, Role) %>%
  summarise( tot = n()) %>%
  group_by(Role) %>%
  mutate(pct = tot/sum(tot))

tab1 <- tab1 %>%
  group_by(Role) %>%
  mutate(pct = tot/sum(tot))


tab1 <- tab1 %>%
  mutate(g_lab = case_when(MetaIntGrReco == 10 ~ "Unions",
                           MetaIntGrReco == 20 ~ "Business Assoc",
                           MetaIntGrReco == 30 ~ "Public Sector Assoc",
                           MetaIntGrReco == 40 ~ "Occupational Assoc",
                           MetaIntGrReco == 50 ~ "Identity Groups",
                           MetaIntGrReco == 60 ~ "Hobby/Leisure Groups",
                           MetaIntGrReco == 70 ~ "Religuous Groups",
                           MetaIntGrReco == 80 ~ "Public Interest Groups",
                           MetaIntGrReco == 90 ~ "Firms",
                           MetaIntGrReco == 100 ~ "Expert Groups",
                           MetaIntGrReco == 110 ~ "Think Tanks",
                           MetaIntGrReco == 120 ~ "Public Sector",
                           MetaIntGrReco == 130 ~ "Political Party/Elective Office",
                           MetaIntGrReco == 140 ~ "Retired"))

p1<- ggplot(tab1, aes(x = reorder(g_lab,pct*100), y = pct*100, 
                 fill = factor(Role))) +
  geom_bar(position = position_dodge(0.7), stat = "identity") +
  theme_classic() +
  coord_flip() +
  scale_fill_manual(values = c("grey", "black"),
                    labels = c("Minister",
                               "Perm.Sec."),
                    name = NULL) +
  labs(x = NULL, y = NULL,
       title = "B: t and t + 1") +
  geom_text(aes(y = (pct*100)+3,
                label = paste(round(pct*100, digits = 1),"%",sep=""), group = Role),
            position = position_dodge(width = 0.7)) +
  geom_text(aes(y = (pct*100)+9,
                label = paste("(", tot,")",sep=""), group = factor(Role)),
            position = position_dodge(width = 0.7)) 

p1

# group type full period
tab2 <- df %>%
  filter(is.na(Jobcoincide) == T & is.na(Singlefewpersonbusinessself) ==T) %>%
  group_by(Role, MetaIntGrReco) %>%
  summarise( tot = n()) %>%
  group_by(Role) %>%
  mutate(pct = tot/sum(tot))

tab2 <- tab2 %>%
  mutate(g_lab = case_when(MetaIntGrReco == 10 ~ "Unions",
                           MetaIntGrReco == 20 ~ "Business Assoc",
                           MetaIntGrReco == 30 ~ "Public Sector Assoc",
                           MetaIntGrReco == 40 ~ "Occupational Assoc",
                           MetaIntGrReco == 50 ~ "Identity Groups",
                           MetaIntGrReco == 60 ~ "Hobby/Leisure Groups",
                           MetaIntGrReco == 70 ~ "Religuous Groups",
                           MetaIntGrReco == 80 ~ "Public Interest Groups",
                           MetaIntGrReco == 90 ~ "Firms",
                           MetaIntGrReco == 100 ~ "Expert Groups",
                           MetaIntGrReco == 110 ~ "Think Tanks",
                           MetaIntGrReco == 120 ~ "Public Sector",
                           MetaIntGrReco == 130 ~ "Political Party/Elective Office",
                           MetaIntGrReco == 140 ~ "Retired"))

p2<- ggplot(na.omit(tab2), aes(x = reorder(g_lab,pct*100), y = pct*100, 
                 fill = factor(Role))) +
  geom_bar(position = position_dodge(0.7), stat = "identity") +
  theme_classic() +
  coord_flip() +
  scale_fill_manual(values = c("grey", "black"),
                    labels = c("Minister",
                               "Perm.Sec."),
                    name = NULL) +
  labs(x = NULL, y = NULL,
       title = "A: Full Post-Elective Period") +
  geom_text(aes(y = (pct*100)+3,
                label = paste(round(pct*100, digits = 1),"%",sep=""), group = Role),
            position = position_dodge(width = 0.7))+
  geom_text(aes(y = (pct*100)+9,
                label = paste("(", tot,")",sep=""), group = factor(Role)),
            position = position_dodge(width = 0.7)) 

p2

legend <- gtable_filter(ggplotGrob(p1), "guide-box") 
full_p1 <- grid.arrange(arrangeGrob(p2 + theme(legend.position="none"), 
                         p1 + theme(legend.position="none"), 
                         nrow = 2,
                         #top = textGrob("Main Title", vjust = 1, gp = gpar(fontface = "bold", cex = 1.5)),
                         bottom = textGrob("Percent of Jobs Held\n per Group Type", vjust = 1)), 
             legend, 
             widths=unit.c(unit(1, "npc") - legend$width, legend$width), 
             nrow=1)

ggdraw(full_p1)

ggsave(full_p1, filename = "Figure3.tiff", device = "tiff",
       dpi = 600,
       width = 8, height = 16)

#-----------------------------
# Figure 4: group type and top jobs
#--------------------------------

#Job_t_role < 1.1 & Job_t_role > -0.1 &

  tab3 <- df %>%
  filter( is.na(Jobcoincide) == T & is.na(Singlefewpersonbusinessself) ==T) %>%
  select(Role, MetaIntGrReco, senior_wo_coinc) %>%
  na.omit() %>%
  group_by(Role, MetaIntGrReco, senior_wo_coinc) %>%
  summarise( tot = n()) %>%
    #group_by(Post, MetaIntGrReco, senior_wo_coinc) %>%
    mutate(pct = tot/sum(tot))

  tab3$top <- ifelse(tab3$senior_wo_coinc == 1, "Senior Position",
                     "Other")

  tab3 <- tab3 %>%
    mutate(g_lab = case_when(MetaIntGrReco == 10 ~ "Unions",
                             MetaIntGrReco == 20 ~ "Business Assoc",
                             MetaIntGrReco == 30 ~ "Public Sector Assoc",
                             MetaIntGrReco == 40 ~ "Occupational Assoc",
                             MetaIntGrReco == 50 ~ "Identity Groups",
                             MetaIntGrReco == 60 ~ "Hobby/Leisure Groups",
                             MetaIntGrReco == 70 ~ "Religuous Groups",
                             MetaIntGrReco == 80 ~ "Public Interest Groups",
                             MetaIntGrReco == 90 ~ "Firms",
                             MetaIntGrReco == 100 ~ "Expert Groups",
                             MetaIntGrReco == 110 ~ "Think Tanks",
                             MetaIntGrReco == 120 ~ "Public Sector",
                             MetaIntGrReco == 130 ~ "Political Party/Elective Office",
                             MetaIntGrReco == 140 ~ "Retired")) 
    
  tab3$Role <- factor(tab3$Role, levels = c(1,2))
  
  tab3$post_lab <- ifelse(tab3$Role==1,"Minister","PermSec")
  
  tab3$y_pos <- tab3$pct
  
  tab3$y_pos <- ifelse(tab3$senior_wo_coinc == 0, 1,tab3$pct)
    
top_pos <- ggplot(tab3, aes(x = reorder(g_lab,tot), y = pct*100, 
                 fill = factor(senior_wo_coinc))) +
  geom_bar(position = "stack", stat = "identity", width = 0.65) +
  theme_classic() +
  coord_flip() +
  scale_fill_manual(values = c("grey", "black"),
                    labels = c("Other",
                               "Senior"),
                    name = NULL) +
  labs(x = NULL, y = "Percent of (Non-)Senior Positions\nBy Group Type",
       title = NULL) +
  facet_wrap(~ post_lab) +
  geom_text(aes(y = (y_pos*100)+5,
                label = paste(round(pct*100, digits = 1),"%",sep=""), group = factor(senior_wo_coinc)))+
  geom_text(aes(y = (y_pos*100)+14,
                label = paste("(",tot,")",sep=""), 
                group = factor(senior_wo_coinc)))

top_pos

ggsave(top_pos, filename = "Figure4.tiff", device = "tiff",
       dpi = 600,
       width = 17, height = 8)

#------------------------------
# Figure 5: Ministries
#-----------------------------

tab4a <- df_collapse %>%
  group_by(MetaMinistrycode, pubpriv) %>%
  summarise(tot = n()) 

tab4b <- df_collapse %>%
  group_by(MetaMinistrycode2, pubpriv) %>%
  summarise(tot2 = n()) 
names(tab4b)[1]<-"MetaMinistrycode"

tab4c <- df_collapse %>%
  group_by(MetaMinistrycode3, pubpriv) %>%
  summarise(tot3 = n()) 
names(tab4c)[1]<-"MetaMinistrycode"

tab4d <- df_collapse %>%
  group_by(MetaMinistrycode4, pubpriv) %>%
  summarise(tot4 = n()) 
names(tab4d)[1]<-"MetaMinistrycode"

tab4e <- df_collapse %>%
  group_by(MetaMinistrycode5, pubpriv) %>%
  summarise(tot5 = n()) 
names(tab4e)[1]<-"MetaMinistrycode"


tab4a <- full_join(tab4a, tab4b, by = c("MetaMinistrycode", "pubpriv"))
tab4a <- full_join(tab4a, tab4c, by = c("MetaMinistrycode", "pubpriv"))
tab4a <- full_join(tab4a, tab4d, by = c("MetaMinistrycode", "pubpriv"))
tab4a <- full_join(tab4a, tab4e, by = c("MetaMinistrycode", "pubpriv"))

tab4a<-as.data.frame(tab4a)

for(i in 3:7){
  tab4a[,i]<-ifelse(is.na(tab4a[,i])==T,0,tab4a[,i])
}
tab4a <- tab4a[-nrow(tab4a) , ]

tab4a$tot <- tab4a$tot + tab4a$tot2 + tab4a$tot3 +tab4a$tot4 + tab4a$tot5

tab4a[,c(4:7)]<-NULL

tab4a <- tab4a %>%
   group_by(MetaMinistrycode) %>%
  mutate(pct = tot/sum(tot))

help <- tab4a %>%
  filter(pubpriv == 1) %>% select(MetaMinistrycode, pct)
names(help)[2]<-"priv_pct"

tab4a <- left_join(tab4a, help, by = "MetaMinistrycode")

tab4a <- tab4a %>%
  mutate(mini_lab = case_when(MetaMinistrycode == 1 ~ "Finance",
                              MetaMinistrycode == 2 ~ "Labor",
                              MetaMinistrycode == 3 ~ "Interior, Education, Equality,\nIntegration, Equality, Research",
                              MetaMinistrycode == 4 ~ "Defense", 
                              MetaMinistrycode == 5 ~ "Justice",
                              MetaMinistrycode == 6 ~ "Church, Culture",
                              MetaMinistrycode == 7 ~ "Environment, Energy",
                              MetaMinistrycode == 8 ~ "Treasury",
                              MetaMinistrycode == 9 ~ "Economics, Business,\nInnovation, Interior",
                              MetaMinistrycode == 10 ~ "Health",
                              MetaMinistrycode == 11 ~ "Foreign Affairs",
                              MetaMinistrycode == 12 ~ "Prime Minister",
                              MetaMinistrycode == 13 ~ "Transport, Housing",
                              MetaMinistrycode == 14 ~ "Other"))

tab4a$priv_pct <- ifelse(tab4a$MetaMinistrycode == 14, 
                         min(tab4a$priv_pct,na.rm=T)-0.1, tab4a$priv_pct)

mini<-ggplot(na.omit(tab4a), 
             aes(x = reorder(mini_lab, priv_pct*100), y = pct*100, 
                  fill = factor(pubpriv))) +
  geom_bar(position = position_dodge(0.7), stat = "identity", 
           colour = "black") +
  theme_classic() +
  coord_flip() +
  scale_fill_manual(values = c("white", "black"),
                    labels = c("Public",
                               "Private"),
                    name = NULL) +
  labs(x = NULL, y = "Percent Jobs Held By Revolvers\nAcross Ministries",
       title = NULL) +
  geom_text(aes(y = (pct*100)+5.5,
                label = paste(round(pct*100, digits = 1),"%",sep=""), group = factor(pubpriv)),
            position = position_dodge(width = 0.7)) +
  geom_text(aes(y = (pct*100)+12,
                label = paste("(",tot,")",sep=""), group = factor(pubpriv)),
            position = position_dodge(width = 0.7))

mini

ggsave(mini, filename = "Figure5.tiff", device = "tiff",
       dpi = 600,
       width = 9, height = 13)


#---------------------------------------
# Figure 6: Parties
#--------------------------------------

tab5 <- df_collapse %>%
  group_by(partyname, pubpriv) %>%
  summarise(tot = n()) %>%
  mutate(tot = ifelse(partyname == 5 & pubpriv == 1, tot + 1, tot)) %>%
  group_by(partyname) %>%
  mutate(pct = tot/sum(tot))

tab5 <- tab5 %>%
  mutate(party_lab = 
           case_when(partyname == 1 ~ "Social Democrats",
                     partyname == 2 ~ "Conservatives",
                     partyname == 3 ~ "Liberals",
                     partyname == 4 ~ "Social Liberals",
                     partyname == 5 ~ "Socialist Peoples Party",
                     partyname == 6 ~ "Liberal Alliance"))
    
help2 <- tab5 %>%
  filter(pubpriv == 1) %>% select(partyname, pct)
names(help2)[2]<-"priv_pct"

tab5 <- left_join(tab5, help2, by = "partyname")


tab6 <- df_collapse %>%
  group_by(partyname, pubpriv_senior) %>%
  summarise(tot = n()) %>%
  mutate(tot = ifelse(partyname == 5 & pubpriv_senior == 2, tot + 1, tot)) %>%
  group_by(partyname) %>%
  mutate(pct = tot/sum(tot))

tab6 <- tab6 %>%
  mutate(party_lab = 
           case_when(partyname == 1 ~ "Social Democrats",
                     partyname == 2 ~ "Conservatives",
                     partyname == 3 ~ "Liberals",
                     partyname == 4 ~ "Social Liberals",
                     partyname == 5 ~ "Socialist Peoples Party",
                     partyname == 6 ~ "Liberal Alliance"))

help3 <- tab6 %>%
  filter(pubpriv_senior == 2) %>% select(partyname, pct)
names(help3)[2]<-"priv_pct"

tab6 <- left_join(tab6, help3, by = "partyname")

tab6$pubpriv_senior <- factor(tab6$pubpriv_senior,
                                     levels = c(1,0,2))

party2<-ggplot(na.omit(tab6), 
               aes(x = reorder(party_lab, priv_pct*100), y = pct*100, 
                   fill = factor(pubpriv_senior))) +
  geom_bar(position = position_dodge(1), stat = "identity",
           colour = "black") +
  theme_classic() +
  coord_flip() +
  scale_fill_manual(values = c("black","white",  "darkgrey"),
                    labels = c("Private, Non-Senior",
                               "Public",
                               "Private, Senior"),
                    name = NULL) +
  labs(x = NULL, y = "Percent of Ministers by Party\nHired by Public vs Private Organizations",
       title = NULL) +
  geom_text(aes(y = (pct*100)+5,
                label = paste(round(pct*100, digits = 1),"%",sep=""), group = factor(pubpriv_senior)),
            position = position_dodge(width = 0.7)) +
  geom_text(aes(y = (pct*100)+11,
                label = paste("(", tot,")",sep=""), group = factor(pubpriv_senior)),
            position = position_dodge(width = 0.7))

party2 

ggsave(party2, filename = "Figure6.tiff", device = "tiff",
       dpi = 600,
       width = 10, height = 8)






